package com.xust.oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xust.oa.domain.entity.KnowledgeBase;
import com.xust.oa.mapper.KnowledgeBaseMapper;
import com.xust.oa.service.KnowledgeBaseService;
import com.xust.oa.utils.SentenceSimilarityNew;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author ZHANGJIE
* @description 针对表【knowledge_base】的数据库操作Service实现
* @createDate 2025-03-25 22:24:35
*/
@Service
public class KnowledgeBaseServiceImpl extends ServiceImpl<KnowledgeBaseMapper, KnowledgeBase>
implements KnowledgeBaseService{

    @Override
    public KnowledgeBase findMostSimilarQuestion(String question) {
        List<KnowledgeBase> list = list();
        Map<String, Integer> vector = SentenceSimilarityNew.getWordFrequencyVector(question);
        for (KnowledgeBase knowledgeBase : list){
            Map<String, Integer> vector1 = SentenceSimilarityNew.getWordFrequencyVector(knowledgeBase.getQuestion());
            double v = SentenceSimilarityNew.cosineSimilarity(vector, vector1);
            if (v>0.75){
                return knowledgeBase;
            }
        }
        return null;
    }
}
